//
//  Log.swift
//
//
//  Created by Jiang Chencheng on 2024/9/14.
//

import Vapor
import Fluent
import struct Foundation.UUID

enum Action: String, Codable {
    case tapPet
    case playWithPet
    case writeDiary
    case deleteDiary
    case uploadPhoto
    case deletePhoto
}

final class Event: Model, @unchecked Sendable {
    
    static let schema = "event"
    
    @ID(key: .id)
    var id: UUID?
    
    @Enum(key: "action")
    var action: Action
    
    @Field(key: "comment")
    var comment: String
    
    @Parent(key: "room_id")
    var room: Room
    
    @Parent(key: "user_id")
    var user: User
    
    @Timestamp(key: "created_at", on: .create)
    var createdAt: Date?

    @Timestamp(key: "updated_at", on: .update)
    var updatedAt: Date?
    
    @Timestamp(key: "deleted_at", on: .delete)
    var deletedAt: Date?
    
    init() { }
    
    init(id: UUID? = nil,roomID: UUID, userID: UUID, action: Action, comment: String = "") {
        self.id = id
        self.$room.id = roomID
        self.$user.id = userID
        self.action = action
        self.comment = comment
    }
    
}


extension Event {
    
    static func log(on db: any Database, roomID: UUID, userID: UUID, action: Action, comment: String = "") async throws {
        let event = Event(roomID: roomID, userID: userID, action: action, comment: comment)
        try await event.save(on: db)
    }
    
}
